﻿package
{
   import flash.display.Sprite;
   import flash.system.Security;
   
   import jp.progression.casts.CastDocument;
   import jp.progression.config.WebConfig;
   import jp.progression.events.ProcessEvent;
   import jp.progression.scenes.SceneId;
   
   import scenes._Root_sc;

   /**
    * @author  cjboy1984@gmail.com
    */
   public class _Index extends CastDocument
   {
      // 過場用的透明擋板
      private var blocker:Sprite = new Sprite();

      // --------------------- LINE ---------------------

      public function _Index()
      {
         super('root', _Root_sc, new WebConfig(null, false, false) );
      }

      // ################### protected ##################

      override protected function atReady():void
      {
         // debug
         //Debugger.addTarget(manager);
         //Locale.language = Locale.JAPANESE;
         //stage.addChild(new Stats()); // benchmark
         //stage.addChild(new MyDebug()); // my debug utils

         // 過場用來擋畫面的blocker
         stage.addChild(blocker);

         // 從FlashVars裡拿 APP_ID, CANVAS_ID 值，參數是 app_id, canvas_id
         var param:Object = stage.loaderInfo.parameters;
         if (param.app_id) DEF.APP_ID = String(param.app_id);
         if (param.canvas_id) DEF.CANVAS_ID = String(param.canvas_id);
         if (param.permission) DEF.PERMISSION = String(param.permission);
         if (param.base_url) DEF.BASE_URL = String(param.base_url);
         
         // allow external js
         Security.allowDomain("*");
         // policy
         Security.loadPolicyFile("http://a1.sphotos.ak.fbcdn.net/crossdomain.xml");
         Security.loadPolicyFile("http://a2.sphotos.ak.fbcdn.net/crossdomain.xml");
         Security.loadPolicyFile("http://a3.sphotos.ak.fbcdn.net/crossdomain.xml");
         Security.loadPolicyFile("http://a4.sphotos.ak.fbcdn.net/crossdomain.xml");
         Security.loadPolicyFile("http://a5.sphotos.ak.fbcdn.net/crossdomain.xml");
         Security.loadPolicyFile("http://a6.sphotos.ak.fbcdn.net/crossdomain.xml");
         Security.loadPolicyFile("http://a7.sphotos.ak.fbcdn.net/crossdomain.xml");
         Security.loadPolicyFile("http://a8.sphotos.ak.fbcdn.net/crossdomain.xml");
         Security.loadPolicyFile("http://profile.ak.fbcdn.net/crossdomain.xml");
         Security.loadPolicyFile("http://img.youtube.com/crossdomain.xml");
         // for developing
         Security.loadPolicyFile(DEF.BASE_URL + "crossdomain.xml");
         
         // facebook
//         if (ExternalInterface.available)
//         {
//            Facebook.init(DEF.APP_ID);
//         }

         // manager
         manager.sync = true; // deep-linking SWFAddress
         manager.addEventListener(ProcessEvent.PROCESS_START, onSceneChangeStart);
         manager.addEventListener(ProcessEvent.PROCESS_SCENE, onSceneChanging);
         manager.addEventListener(ProcessEvent.PROCESS_COMPLETE, onSceneChangeComplete);
         trace('-----> syncedSceneId =', manager.syncedSceneId);
         // root scene 只裝了共用元件，判斷如果syncedSceneId == root scene，就導到 /#/home 去！
         if (manager.syncedSceneId.equals(manager.root.sceneId))
         {
            // goto /root/home
            manager.goto(new SceneId('/root/home'));

            // >test<
            //manager.goto(new SceneId('/root/test'));
         }
         else
         {
            // 移掉路徑上所有的'/float'點！
            var newSyncPath:String = manager.syncedSceneId.path.replace(/\/float*/g, "");
            manager.goto(new SceneId(newSyncPath));
         }
      }

      // #################### private ###################

      private function onSceneChangeStart(e:ProcessEvent):void
      {
         trace("----- start -----");
         // 先確定 blocker 永遠在最上層
         stage.setChildIndex(blocker, stage.numChildren - 1);

         blocker.graphics.clear();
         blocker.graphics.beginFill(0x0000FF, 0);
         blocker.graphics.drawRect(0, 0, sw, sh);
         blocker.graphics.endFill();
      }

      private function onSceneChanging(e:ProcessEvent):void
      {
         var nowPath:String = manager.currentSceneId.path;
         trace(">> onSceneChanging << to,", nowPath);
      }

      private function onSceneChangeComplete(e:ProcessEvent):void
      {
         trace("----- complete -----");
         blocker.graphics.clear();
         
         // bgm control sample
//         switch(manager.currentSceneId.path)
//         {
//            case '/root/intro':
//            case '/root/tvc':
//               if (bgm && bgm.playing)
//               {
//                  bgm.stopSound();
//               }
//               break;
//            default:
//               if (bgm && !bgm.playing)
//               {
//                  bgm.playSound();
//               }
//               break;
//         }
      }

      // --------------------- LINE ---------------------

      private function get sw():Number { return stage.stageWidth; }
      private function get sh():Number { return stage.stageHeight; }

      // --------------------- LINE ---------------------

   }

}
